package com.huawei.esdk.uc.sametime.widgets;

import org.apache.log4j.Logger;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

/**
 * 创建来电弹窗单例类
 * @author lWX169831
 * 
 */
public class TipPopup
{
    private static Logger logger = Logger.getLogger(TipPopup.class);

    private Shell shell;

    private int moveStep = 2; // 每次移动的pixel

    private int upPosition; // 能移动到的最上面坐标

    private int downPosition; // 当前popup的边框坐标

    private int leftPosition; // popup左边边框坐标

    private Label messageLable;

    private boolean flag = false;

    private Thread thread;

    private Runnable runnable;

    private String messageText;

    public TipPopup()
    {
        runnable = new Runnable()
        {
            @Override
            public void run()
            {
                Display display = null;
                while (flag)
                {
                    try
                    {
                        Thread.sleep(10);

                        // 判断当前位置是否小于能出现的最高位置，小于的话就说明还可以向上移动。
                        if ((downPosition - moveStep) > upPosition)
                        {
                            // 存在通话弹出窗口向上弹出过程中,通话结束导致shell为空的问题
                            if (null != shell)
                            {
                                display = shell.getDisplay();
                                display.asyncExec(new Runnable()
                                {
                                    @Override
                                    public void run()
                                    {
                                        shell.setLocation(leftPosition,
                                                downPosition - moveStep);
                                        downPosition -= moveStep;
                                    }
                                });
                            }
                        }
                        else
                        {
                            // 此时已经移动到了最高位置，显示5秒钟后，关闭窗口并退出。
                            Thread.sleep(5000);
                            display.asyncExec(new Runnable()
                            {
                                @Override
                                public void run()
                                {
                                    closeTipPopup();
                                }
                            });
                        }
                    }
                    catch (InterruptedException exception)
                    {
                        logger.error(exception.toString());
                    }
                }
            }
        };
    }

    /**
     * 创建来电的弹出窗口
     */
    private void createPopup()
    {
        shell = new Shell(SWT.ON_TOP);

        messageLable = new Label(shell, SWT.CENTER);
        messageLable.setBounds(25, 25, 195, 25); // (left, top, width, high)
        messageLable.setText(messageText);

        shell.setSize(245, 75);
        // 取屏幕大小
        Rectangle area = Display.getDefault().getClientArea();
        upPosition = area.height - (75);// 计算出popup界面在屏幕显示的最高位置
        downPosition = area.height + (75);// 计算出popup界面的初始位置
        leftPosition = area.width - 250;
        // 初始化popup位置
        shell.setLocation(leftPosition, downPosition);
        shell.open();
    }

    public void showTipPopup(String messageText)
    {
        this.messageText = messageText;
        if (null == shell)
        {
            createPopup();
        }

        thread = new Thread(runnable);
        flag = true;
        thread.start();
    }

    public void closeTipPopup()
    {
        flag = false;
        if (null != shell)
        {
            logger.debug("try to close call window");
            shell.close();
            shell = null;
        }
        if (null != thread)
        {
            thread = null;
        }
    }

}
